package com.hksj.Intelligentmanagement.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.LampControlDTO;
import com.hksj.Intelligentmanagement.utils.MemoryPagination;
import com.hksj.Intelligentmanagement.vo.LampControlVo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.*;


/**
 * @ClassName LampControlService
 * @Description TODO
 * @Author @陈章
 * @Date 2023/4/6 15:11
 * @Version 1.0
 */
@Service
public class LampControlService {

    public Result control(LampControlDTO lampControlDTO) {
        //请求的 urlhttp://114.116.54.247:3000/orSet/
        String url = "http://114.116.54.247:3000/orSet/";

        //创建一个请求工厂
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000*30);//设置请求连接超时时间3s
        requestFactory.setReadTimeout(1000*30);//设置读取超时时间3s

        //创建请求发送模板实例
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);

        // 设置请求参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("cmd", lampControlDTO.getCmd());

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/x-www-form-urlencoded");

        //请求体中包含的参数设置
        HttpEntity<Object> httpEntity = new HttpEntity<>(params,headers);

        //使用restTemplate发送post请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);

        //获取请求结果
        String result = responseEntity.getBody();
        //将结果转换为json
        Object jsonResult = JSON.parse(result);

        //对返回的结果进行判断,如果结果包含yes则调用成功,包含no则调用失败
        if (result.contains("Yes")){
            return Result.ok(jsonResult);
        }

        return Result.error().setData(jsonResult);

    }

    //查询设备状态
    public Result queryInfosByPaging(LampControlDTO lampControlDTO){

        //请求的 urlhttp://114.116.54.247:3000/orSet/
        String url = "http://114.116.54.247:3000/orInfo/";

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000*30);//设置请求连接超时时间30s
        requestFactory.setReadTimeout(1000*30);//设置读取超时时间30s

        //创建请求发送模板实例
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);

        //发送get请求,参数为空
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);

        //获取响应体
        String result = responseEntity.getBody();

        /*//转换为json格式 {"msg":[{},{},{}......]}
        JSONObject jsonObject = JSONObject.parseObject(result);*/

        JSONObject jsonObject = JSONObject.parseObject(result);

        //将结果转为 字符串型的jsonArray
        String msg = jsonObject.get("msg").toString();

        //将响应体转换为 jsonArray
        JSONArray jsonArray = JSON.parseArray(msg);

        //将jsonArray转为List<LampControlVo>
        List<LampControlVo> lampControlVos = jsonArray.toJavaList(LampControlVo.class);

        //创建page分页示例
        IPage<LampControlVo> page = new Page<>(lampControlDTO.getPageNum(),lampControlDTO.getPageSize());

        //创建内存分页示例
        MemoryPagination<LampControlVo> memoryPagination = new MemoryPagination<>(page);

        //分页
        List<LampControlVo> pagination = memoryPagination.pagination(lampControlVos, lampControlDTO.getPageNum(),lampControlDTO.getPageSize(), page);

        //将分页结果设置到分页实例中
        page.setRecords(pagination)
                .setCurrent(memoryPagination.getPage().getCurrent())//将分页的当前页设置到page当前页
                .setPages(memoryPagination.getPage().getPages())//将分页总页数设置到page的总页数
                .setTotal(memoryPagination.getPage().getTotal());//将记录总条数设置到page总条数

        //返回分页结果
        return Result.data(page);

    }

}
